package com.kaifamiao.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 没有使用循环是期望理解 next 方法 与 remove 的关系
 */
public class TestRemove {

    public static void main(String[] args) {

        Collection<Integer> x = new ArrayList<>();
        x.add( 10 );
        x.add( 21 );
        x.add( 22 );
        x.add( 30 );
        x.add( 2 );
        System.out.println( x ); // x == null ? "null" : x.toString()

        x.remove( 2 );
        System.out.println( x );

        Iterator<Integer> itor = x.iterator();
        // itor.remove(); // java.lang.IllegalStateException
        Integer e = itor.next();
        System.out.println( e );
        System.out.println( x );

        itor.remove();
        System.out.println( x );

        System.out.println( itor.next() );
        itor.remove();
        System.out.println( x );

        System.out.println( itor.next() );
        itor.remove();
        System.out.println( x );

        System.out.println( itor.next() );
        itor.remove();
        System.out.println( x );

        // System.out.println( itor.next() ); // java.util.NoSuchElementException
        System.out.println( itor.hasNext() );

    }

}
